$(document).ready(function(){
    $('.orientation input').click(function(){
        var orientation = $(this).val();
        var $div = $("#drownBox");
        var $status = $('#statusBlock #orient');
        if (orientation == 'P') {
            $div.removeClass('horizontal').addClass('vertical');
            $status.text('Vertical')
        } else {
            $div.removeClass('vertical').addClass('horizontal');
            $status.text('Horizontal')
        }
    })
    $('.dimension input').click(function(){
        var size = $(this).val();
        var $div = $("#drownBox");
        var $status = $('#statusBlock #inches');
        if (size == 1) {
            $div.removeClass('inch24').addClass('inch18');
            $status.text('18"x24"')
        } else {
            $div.removeClass('inch18').addClass('inch24');
            $status.text('24"x36"')
        }
    })
    
    $('#workDiv1').draggable({
        containment: "#drownBox", 
        scroll: false,
        start: function(event, ui) {
            $(this).addClass('activeDiv').siblings().removeClass('activeDiv');
        }
    })
    $("#drownBox .workDivs").live('click',function(){
        $(this).addClass('activeDiv').siblings().removeClass('activeDiv');
    })
    $('.lineText input:text').focus(function(){
        if ($(this).val()=='Enter Text Line') {
            $(this).val('');
        }
    })
    $('.lineText input:text').blur(function(){
        if ($(this).val()=='') {
            $(this).val('Enter Text Line');
        }
    })
    $('.lineText input:checkbox').live('click',function(){
        var parent = $(this).parents('.lineText');
        var id = 'workDiv'+$(this).attr('data');
        var workDiv = $('#'+id);
        if (!workDiv.length) {
            workDiv = $('#workDiv1').clone().attr('id',id).appendTo("#drownBox").addClass('activeDiv').hide()
                .text(parent.find('input:text').val())
                    .draggable({containment: "#drownBox", scroll: false,
                        start: function() {$(this).addClass('activeDiv').siblings().removeClass('activeDiv');}
                    })
                    .css({'top':'10px'});
        }
        if ($(this).attr('checked')=='checked') {
            workDiv.show().addClass('activeDiv').text(parent.find('input:text').val()).siblings().removeClass('activeDiv');
        } else {
            workDiv.hide().removeClass('activeDiv');
        }
//        workDiv.toggle().addClass('activeDiv').text(parent.find('input:text').val()).siblings().removeClass('activeDiv');
    })
    
    $('.lineText input:text').keyup(function(){
        var parent = $(this).parents('.lineText');
        var checkbox = parent.find('input:checkbox');
        var id = '#workDiv'+checkbox.attr('data');
        var workDiv = $(id);
        workDiv.text($(this).val()).addClass('activeDiv').siblings().removeClass('activeDiv');
    })
    
    $('.addNewLine a').live ('click',function(){
        if ($('.lineText').length <= 6) {
            var text = $('#baseTextInput').clone(true).removeAttr('id').insertAfter('.lineText:last').find('input:checkbox').attr({'data':$('.lineText').length,'checked':false});
            $('.lineText:last').val('Enter Text Line')
            $.uniform.restore("input:checkbox");
            $("input:checkbox").uniform();
        }
        return false;
    })
    
    $('.colorSelect').live('click',function(){
        $(this).addClass('activeColorBox').siblings().removeClass('activeColorBox');
        $('#drownBox .activeDiv').css('color',$(this).attr('data'));
        
    })
    
    $('#sizeSelect').change(function(){
        var workDiv = $("#drownBox .activeDiv");
        var data = parseInt($(this).find('option:selected').attr('data')) + 10;
        workDiv.css('font-size',data+'px')
    })
    
    $('#fontSelect').change(function(){
        var workDiv = $("#drownBox .activeDiv");
        var font = $(this).find('option:selected').attr('data');
        workDiv.css('font-family',font)
    })
    
    $('.arrowsDirection').live('click',function(){
        var selected = $(this).attr('data');
        var left = parseInt($('#drownBox .activeDiv').css('left'));
        var top = parseInt($('#drownBox .activeDiv').css('top'));
        if (selected == 'right') {
            $('#drownBox .activeDiv').css('left',left+10);
        } else if (selected == 'left') {
            $('#drownBox .activeDiv').css('left',left-10);
        } else if (selected == 'down') {
            $('#drownBox .activeDiv').css('top',top+10);
        } else if (selected == 'up') {
            $('#drownBox .activeDiv').css('top',top-10);
        }
    })
    $('#saveAndContinue').live('click',function(){
        var values = $('#customForm').serialize();
        var boxes = {};
        var i = 0;
        //Select Active divs
        $('#drownBox .workDivs:visible').each(function(){
            boxes[i] = {'font-family': $(this).css('font-family'),
                'size':$(this).css('font-size'),'color':$(this).css('color'),
                'top':$(this).css('top'),'left':$(this).css('left'),
                'text':$(this).text()}
            i++;
        })
        values += '&'+$.param(boxes);
        $.ajax({
            url:'/custom/savesign/',
            data:values,
            type:'POST',
            dataType:'json',
            success:function(response) {
                if (response.result == true) {
                    window.location.assign('/custom/save');
                }
            }
        })
        return false;
    })
    $('#saveSign').live('click',function(){
        var values = $('#customForm').serialize();
        var boxes = {};
        var i = 0;
        //Select Active divs
        $('#drownBox .workDivs:visible').each(function(){
            boxes[i] = {'font-family': $(this).css('font-family'),
                'size':$(this).css('font-size'),'color':$(this).css('color'),
                'top':$(this).css('top'),'left':$(this).css('left'),
                'text':$(this).text()}
            i++;
        })
        values += '&'+$.param(boxes);
        $.ajax({
            url:'/custom/savesign/',
            data:values,
            type:'POST',
            dataType:'json',
            success:function(response) {
                alert ('Sign saved')
            }
        })
        return false;
    })
    $('#goBillingCustom').click(function(){
        if($('#termsAgrea').attr('checked') != 'checked' ) {
            alert ('You should agry with our terms of service, to continue!');
            return false;
        }
        $('#customFormSave').submit();
        return false;
    })
})
